import { disabledRoleApi, enabledRoleApi, getListApi } from "@/api/managementCenter/roleAndPermission";
import { Modal, message } from "ant-design-vue";
import { createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { searchForm, tableData, drawerOpts, loading } = state;
  const getData = () => {
    loading.value = true;
    return getListApi(searchForm)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const searchHandle = () => {
    searchForm.page = 1;
    getData();
  };
  const changeStatusHandle = (row: RoleAndPermission.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null,
      requestFn: any = null;
    if (row.status === 1) {
      tip = "是否停用该角色?";
      requestFn = disabledRoleApi;
    } else {
      tip = "是否启用该角色?";
      requestFn = enabledRoleApi;
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        requestFn(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            getData();
          }
        });
      }
    });
  };
  const addHandle = () => {
    drawerOpts.title = "新增角色";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "AddRole";
    drawerOpts.visible = true;
  };
  const editHandle = (row: RoleAndPermission.ListRows) => {
    drawerOpts.title = "编辑角色";
    drawerOpts.width = "1200px";
    drawerOpts.passData = row;
    drawerOpts.type = 2;
    drawerOpts.component = "AddRole";
    drawerOpts.visible = true;
  };
  const drawerSubmitHandle = () => {
    drawerOpts.visible = false;
    getData();
  };
  return {
    searchHandle,
    getData,
    changeStatusHandle,
    editHandle,
    drawerSubmitHandle,
    addHandle
  };
}
